﻿using System;
using System.Windows;
using System.Windows.Controls;
using WickedFlame.Core.Layers;

namespace WickedFlame.Core
{
	/// <summary>
	/// Base class for all drawing canvases and layers
	/// </summary>
    public abstract class CanvasBase :  Panel
    {
        protected override Size ArrangeOverride(Size arrangeSize)
        {
            foreach (UIElement element in base.InternalChildren)
            {
                if (element == null)
                {
                    continue;
                }

                // only allow elements that derive Layer
                if (!(element is Layer))
                    throw new Exception(string.Format("Element is {0} but should be of type VecDraw.Core.Layers.Layer!", element.ToString()));

                element.Arrange(new Rect(new Point(0, 0), element.DesiredSize));
            }

            //Size size2 = new Size(2, 2);
            //this.InternalTransform = new ScaleTransform(size2.Width, size2.Height);
            //arrangeSize.Width = size2.Width * this.Width;
            //arrangeSize.Height = size2.Height * this.Height;

            return arrangeSize;
        }

        protected override Size MeasureOverride(Size constraint)
        {
            Size availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
            foreach (UIElement element in base.InternalChildren)
            {
                if (element != null)
                {
                    element.Measure(availableSize);
                }
            }

            // take all the space that is given by the parent
            return new Size(0,0);
        }

        protected override System.Windows.Media.Geometry GetLayoutClip(Size layoutSlotSize)
        {
            if (base.ClipToBounds)
            {
                return new System.Windows.Media.RectangleGeometry(new Rect(base.RenderSize));
            }
            return null;
        }



        //private Transform InternalTransform
        //{
        //    get
        //    {
        //        return this.InternalVisual.Transform;
        //    }
        //    set
        //    {
        //        this.InternalVisual.Transform = value;
        //    }
        //}
        //private ContainerVisual InternalVisual
        //{
        //    get
        //    {
        //        if (this._internalVisual == null)
        //        {
        //            this._internalVisual = new ContainerVisual();
        //            base.AddVisualChild(this._internalVisual);
        //        }
        //        return this._internalVisual;
        //    }
        //}
        //private ContainerVisual _internalVisual;




        
        //[AttachedPropertyBrowsableForChildren, TypeConverter("System.Windows.LengthConverter, PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
        //public static double GetLeft(UIElement element)
        //{
        //    if (element == null)
        //    {
        //        throw new ArgumentNullException("element");
        //    }
        //    return (double)element.GetValue(LeftProperty);
        //}
    }
}
